<?php

namespace App\Models\Admin;

use App\Scopes\SaasScope;
use Illuminate\Database\Eloquent\Model;

class GoodsSpu extends Model
{

    protected $table = 'goods_spu';

    protected $dateFormat = 'U';

    public $timestamps = true;

    protected $guarded = [];

    public function goods()
    {
        return $this->belongsTo(Goods::class,'goods_id','id');
    }

    public function spuUnit()
    {
        return $this->hasOne(GoodsDatum::class,'id','unit');
    }

    public function spuName()
    {
        return $this->hasOne(GoodsDatum::class,'id','spu_name');
    }

    public static function info($id=0,$type='',$value=0)
    {
        if (!is_numeric($id)){
            return false;
        }
        $info = self::findOrFail($id);
        if (empty($type)){
            return false;
        }
        $info->$type = $value;
        $preInfo = $info->getOriginal();
        $info->save();
        return $preInfo;
    }

    public static function deleted($id=0):array
    {
        $info = self::find($id);
        if ($info->standard_spu_id && $info->standard_spu_code){
            return ['error','40021','不能删除标准商品规格'];
        }
        if($info->base){
            return ['error','40022','不能删除商品基础属性'];
        }
        if ($info->delete()){
            return ['success','0','删除成功'];
        }
        return ['error','40011','删除失败'];
    }
}
